//
//  EPCVisitorView.swift
//  Weibo
//
//  Created by LioWu on 05/04/2017.
//  Copyright © 2017 MyGit. All rights reserved.
//

import UIKit

class EPCVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    var visitorInfo: [String: String]? {
        
        didSet {
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    return
            }
            
            // message
            tipTitleLabel.text = message
            
            // home
            if imageName == "" {
                
                startAnimation()
                
                return
            }
            
            iconImageView.image = UIImage.init(named: imageName)
            iconMackImageView.isHidden = true
            houseImageView.isHidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func startAnimation() {
        let anim = CABasicAnimation.init(keyPath: "transform.rotation")
        anim.fromValue = 2 * M_PI
        anim.toValue = 0
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.isRemovedOnCompletion = false
        
        iconImageView.layer.add(anim, forKey: nil)
    }
    
    
    // MARK: subviews
    
    // registerButton
    lazy var registerButton:UIButton = UIButton.cz_textButton(
        "注册",
        fontSize: 14,
        normalColor: UIColor.orange,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")
    
    // loginButton
    lazy var loginButton:UIButton = UIButton.cz_textButton(
        "登录",
        fontSize: 14,
        normalColor: UIColor.darkGray,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")
    
    
    // iconImage
    fileprivate lazy var iconImageView:UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
    
    // iconMackImageView
    fileprivate lazy var iconMackImageView:UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
    
    
    // houseImage
    fileprivate lazy var houseImageView:UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_house"))
    
    // tipTitle
    fileprivate lazy var tipTitleLabel:UILabel = UILabel.cz_label(
        withText: "阿里斯顿福君奥说，阿斯顿发生大芬，阿萨德",
        fontSize: 14,
        color: UIColor.darkGray)
    
    
}

extension EPCVisitorView {
    
    func setupUI() {
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        addSubview(iconImageView)
        addSubview(iconMackImageView)
        addSubview(houseImageView)
        addSubview(tipTitleLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        tipTitleLabel.textAlignment = .center
        
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // iconImageView
        addConstraint(NSLayoutConstraint.init(
            item: iconImageView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint.init(
            item: iconImageView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerY,
            multiplier: 1.0,
            constant: -60))
        
        
        // iconMackImageView
//        addConstraint(NSLayoutConstraint.init(
//            item: iconMackImageView,
//            attribute: .centerX,
//            relatedBy: .equal,
//            toItem: iconImageView,
//            attribute: .centerX,
//            multiplier: 1.0,
//            constant: 0))
//        
//        addConstraint(NSLayoutConstraint.init(
//            item: iconMackImageView,
//            attribute: .centerY,
//            relatedBy: .equal,
//            toItem: iconImageView,
//            attribute: .centerY,
//            multiplier: 1.0,
//            constant: 0))
        
        let maskImageView_H_Dict = ["iconMackImageView": iconMackImageView,
                                    "loginButton": loginButton] as [String : Any]
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[iconMackImageView]-0-|",
            options: [],
            metrics: nil,
            views: maskImageView_H_Dict))
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-0-[iconMackImageView]-(-20)-[loginButton]",
            options: [],
            metrics: nil,
            views: maskImageView_H_Dict))
        
        
        // houseImageView
        addConstraint(NSLayoutConstraint.init(
            item: houseImageView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: iconImageView,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint.init(
            item: houseImageView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: iconImageView,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0))
        
        // tipLabel
        addConstraint(NSLayoutConstraint.init(
            item: tipTitleLabel,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: iconImageView,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint.init(
            item: tipTitleLabel,
            attribute: .top,
            relatedBy: .equal,
            toItem: iconImageView,
            attribute: .bottom,
            multiplier: 1.0,
            constant: 20))
        
        addConstraint(NSLayoutConstraint.init(
            item: tipTitleLabel,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: 216))
        
        // registerButton
        addConstraint(NSLayoutConstraint.init(
            item: registerButton,
            attribute: .left,
            relatedBy: .equal,
            toItem: tipTitleLabel,
            attribute: .left,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint.init(
            item: registerButton,
            attribute: .top,
            relatedBy: .equal,
            toItem: tipTitleLabel,
            attribute: .bottom,
            multiplier: 1.0,
            constant: 20))
        
        addConstraint(NSLayoutConstraint.init(
            item: registerButton,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: 100))
        
        // loginButton
        addConstraint(NSLayoutConstraint.init(
            item: loginButton,
            attribute: .right,
            relatedBy: .equal,
            toItem: tipTitleLabel,
            attribute: .right,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint.init(
            item: loginButton,
            attribute: .top,
            relatedBy: .equal,
            toItem: tipTitleLabel,
            attribute: .bottom,
            multiplier: 1.0,
            constant: 20))
        
        addConstraint(NSLayoutConstraint.init(
            item: loginButton,
            attribute: .width,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: 100))
        
//        addConstraint(NSLayoutConstraint.init(
//            item: <#T##Any#>,
//            attribute: <#T##NSLayoutAttribute#>,
//            relatedBy: <#T##NSLayoutRelation#>,
//            toItem: <#T##Any?#>,
//            attribute: <#T##NSLayoutAttribute#>,
//            multiplier: <#T##CGFloat#>,
//            constant: <#T##CGFloat#>))
        
    }
}
